package com.manager.service.impl;

import com.manager.bean.Building;
import com.manager.bean.BuildingExample;
import com.manager.bean.PageInfo;
import com.manager.mapper.BuildingMapper;
import com.manager.service.BuildingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Building)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:00:23
 */
@Service("buildingService")
public class BuildingServiceImpl implements BuildingService {
    @Resource
    private BuildingMapper buildingDao;

    @Override
    public PageInfo<Building> findPageInfo(Integer pageIndex, Integer pageSize) {
        PageInfo<Building> pi = new PageInfo<Building>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        BuildingExample buildingExample=new BuildingExample();
        BuildingExample.Criteria criteria=buildingExample.createCriteria();
        //获取总条数
        Integer totalCount = buildingDao.countByExample(buildingExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<Building> buildings =buildingDao.selectByExample(buildingExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>buildings.size()-1){
                end=buildings.size();
            }
            buildings=buildings.subList(start,end);
            pi.setList(buildings);
        }
        return pi;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Building queryById(Integer id) {
        return this.buildingDao.selectByPrimaryKey(id);
    }


    @Override
    public List<Building>  getAll(){
        BuildingExample buildingExample = new BuildingExample();
        BuildingExample.Criteria criteria = buildingExample.createCriteria();
        return buildingDao.selectByExample(buildingExample);
    }

    /**
     * 新增数据
     *
     * @param building 实例对象
     * @return 实例对象
     */
    @Override
    public Building insert(Building building) {
        this.buildingDao.insert(building);
        return building;
    }

    /**
     * 修改数据
     *
     * @param building 实例对象
     * @return 实例对象
     */
    @Override
    public Building update(Building building) {
        this.buildingDao.updateByPrimaryKey(building);
        return this.queryById(building.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.buildingDao.deleteByPrimaryKey(id) > 0;
    }
}
